﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Person.ServiceReference1;
using System.Data.Services.Client;
using System.Collections;

namespace Person.View
{
    /// <summary>
    /// Interaction logic for PersonView.xaml
    /// </summary>
    public partial class PersonView : UserControl
    {
        ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://localhost:1179/odata"));
        public PersonView()
        {
            InitializeComponent();

            this.membersListView.ItemsSource = (IEnumerable)context.Members;
            
            CollectionViewSource membersViewSource = (CollectionViewSource)this.Resources["membersViewSource"];
            membersViewSource.Source = context.Members;

            this.comboBoxComId.Items.Add("1");
            this.comboBoxComId.Items.Add("2");
            this.comboBoxComId.Items.Add("3");
            this.comboBoxComId.Items.Add("4");
            this.comboBoxComId.Items.Add("5");

        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            Object obj = new object();
            Member newMember = new Member { Mem_Id = Int32.Parse(this.txtId.Text), Mem_Name = this.txtName.Text, Mem_Tel = this.txtTel.Text, Mem_Email = this.txtEmail.Text, Mem_Addr = this.txtAddr.Text, Com_Id = (this.comboBoxComId.SelectedIndex+1) };
            context.AddToMembers(newMember);
            context.SaveChanges();

            DataServiceCollection<Member> lstContext = new DataServiceCollection<Member>();

            lstContext.LoadCompleted += (objsender, eventArg) =>
            {
                if (eventArg.Error == null)
                {
                    this.membersListView.ItemsSource = (IEnumerable)lstContext;
                }
            };

            lstContext.LoadAsync(context.Members);
       }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            Member member = context.Members.Where(kkk => kkk.Mem_Id == Int32.Parse(this.txtId.Text)).SingleOrDefault();
            member.Mem_Name = this.txtName.Text;
            member.Mem_Tel = this.txtTel.Text;
            member.Mem_Email = this.txtEmail.Text;
            member.Mem_Addr = this.txtAddr.Text;
            member.Com_Id = this.comboBoxComId.SelectedIndex + 1;

            context.UpdateObject(member);
            context.SaveChanges();
            
            DataServiceCollection<Member> lstContext = new DataServiceCollection<Member>();

            lstContext.LoadCompleted += (objsender, eventArg) =>
            {
                if (eventArg.Error == null)
                {
                    this.membersListView.ItemsSource = (IEnumerable)lstContext;
                }
            };

            lstContext.LoadAsync(context.Members);

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            
            var index = this.membersListView.SelectedIndex;
            Object member = this.membersListView.Items.GetItemAt(index);
            context.DeleteObject(member);
            context.SaveChanges();

            DataServiceCollection<Member> lstContext = new DataServiceCollection<Member>();

            lstContext.LoadCompleted += (objsender, eventArg) =>
            {
                if (eventArg.Error == null)
                {
                    this.membersListView.ItemsSource = (IEnumerable)lstContext;
                }
            };

            lstContext.LoadAsync(context.Members);
        }
    }
}
